﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QuanLyNhaSach.QuanLyNhaSachDTO;
namespace QuanLyNhaSach.QuanLyNhaSachDao
{
    class LoaiSachDao
    {
        public static DataTable GetLoaiSachAll()
        {
            string sql = "select * from LoaiSach where Active=True";
            return Database.ExcuQuery(sql);
        }
        public static bool KTTen(string ten)
        {
            string sql = "select * from LoaiSach ";
            DataTable dt = Database.ExcuQuery(sql);
            for (int i=0;i<dt.Rows.Count;i++)
                if (dt.Rows[i]["TenLoaiSach"].ToString() == ten && dt.Rows[i]["Active"].ToString() == "false")
            {
                return true;
            }
            return false;
        }
        public static bool KTTenTT(string ten)
        {
            string sql = "select * from LoaiSach ";
            DataTable dt = Database.ExcuQuery(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
                if (dt.Rows[i]["TenLoaiSach"].ToString() == ten && dt.Rows[i]["Active"].ToString() == "True")
                {
                    return true;
                }
            return false;
        }
        public static bool Insert(LoaiSachDTO tl)
        {
            string sql;
            bool active = true;
            if (KTTen(tl.TenLoaiSach) == true)
            {
                sql = "Update  LoaiSach set Active ='" + active + "'where TenLoaiSach=" + tl.TenLoaiSach + "";
                Database.ExcuNonQuery(sql);
                return true;
            }
            if (KTTenTT(tl.TenLoaiSach) == false && KTTen(tl.TenLoaiSach) == false)
            {
                sql = "insert into LoaiSach(TenLoaiSach,Active) values('" + tl.TenLoaiSach + "',"+ active + ")";

                Database.ExcuNonQuery(sql);
                return true;
            }
            
                return false;
           
        }
        public static void Delete(LoaiSachDTO tl)
        {
            bool active = false;
            string sql = "Update  LoaiSach set Active = " + active + " where TenLoaiSach= '" + tl.TenLoaiSach + "'";
            Database.ExcuNonQuery(sql);
        }
        public static void Update(LoaiSachDTO tl)
        {
            string sql = "Update  LoaiSach set TenLoaiSach = '" + tl.TenLoaiSach + "'where MaLoaiSach= " + tl.MaLoaiSach + "";
            Database.ExcuNonQuery(sql);
        }
    }
}
